package cn.edu.lingnan.lsqn.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import cn.cdahua.basic.dao.BaseDao;
import cn.edu.lingnan.lsqn.model.Column;
import cn.edu.lingnan.lsqn.model.Group;
import cn.edu.lingnan.lsqn.model.GroupColumn;

@Repository("columnDao")
public class ColumnDao extends BaseDao<Column> implements IColumnDao {

	@Override
	public void addGroupColumn(Group group, Column column) {
		this.getSession().save(new GroupColumn(group,column));
	}

	@Override
	public void deleteGroupColumn(int c_id) {
		String hql = "delete GroupColumn gc where gc.column.id = ?";
		updateByHql(hql,c_id);
	}

	@Override
	public List<Column> listColumnParent() {
		String hql = "from Column c where c.column is null";
		return this.list(hql);
	}

	@Override
	public List<Column> listByParent(Integer pid) {
		String hql = "from Column c where c.parent.id = ?";
		return this.list(hql,pid);
	}

	@Override
	public List<Column> list() {
		String hql = "from Column";
		return super.list(hql);
	}

	@Override
	public int getMaxOrder(Integer pid) {
		String hql = "select max(c.orders) from Column c where c.parent.id="+pid;
		if(pid==null||pid==0) hql = "select max(c.orders) from Column c where c.parent is null";
		Object obj = this.queryObject(hql);
		if(obj==null) return 0;
		return (Integer)obj;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Column> listByGroup(Integer gid) {
		String hql = "select gc.column from GroupColumn gc where gc.group.id = ?";
		return this.getSession().createQuery(hql).setParameter(0, gid.intValue()).list();
	}
}
